package org.fessingapore.library.controllers;

import java.beans.PropertyEditorSupport;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.fessingapore.library.models.Category;
import org.fessingapore.library.models.Item;
import org.fessingapore.library.services.CategoryService;
import org.fessingapore.library.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/item")
public class ItemController {

	private ItemService itemService;
	private CategoryService categoryService;

	@Autowired
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	@Autowired
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@InitBinder
	public void bind(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				try {
					setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
					return;
				} catch (ParseException e) {
				}
				try {
					setValue(new SimpleDateFormat("yyyy").parse(value));
					return;
				} catch (ParseException e) {
				}
				setValue(null);
			}
			public String getAsText() {
				if (getValue() == null)
					return null;
				else
					return new SimpleDateFormat("yyyy-MM-dd")
							.format((Date) getValue());
			}

		});
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String searchItem(@RequestParam("q") String searchTerm, Model model) {
		// service.search(searchTerm);
		return "result";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getById(@PathVariable String id) {
		return "";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String viewAddItemForm(Model model) {
		Item item = new Item();
		List<Category> categories = categoryService.findAll();
		model.addAttribute("item", item);
		model.addAttribute("allCategories", categories);
		return "item/addItem";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addItem(Item item, Model model) {
		Item result = itemService.create(item);
//		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
//		Directory d = FSDirectory.open(new File (""));
		return "redirect:/item";
	}

}
